<?php
class owner extends CI_Controller{
	private $data;
	public function __construct(){
		parent::__construct();
		if(!Auth::isLoggedIn()) redirect("/");
		$this->load->model("model_owner");
	}
		
	/*
			=================Function Add Driver=================
	*/
	public function index(){
		$data['owning'] = 'y';
		$data['masterinput'] = 'y';
		$data['title'] = TITLE.' - Tambah Data Agency/Leasing';
		if($this->input->post("btnSave") ){
			
			$hasil = $this->_addNewOwner();
			if($hasil) $data['success'] = "Leasing Company/Agency berhasil Ditambahkan";
			
		}
		$this->load->view("ownerAdd",$data);
		
	}
	
	
	
	function _addNewOwner(){
		//$this->form_validation->set_message('_checkpassword', 'Current Password is incorrect');
		$this->form_validation->set_message('_checkCompAva', 
							'Perusahaan Leasing/Agency sudah pernah diinput sebelumnya');
		$this->form_validation->set_error_delimiters('', '');
		$config = array(
               	array(
                     'field'   => 'nama',
                     'label'   => 'Nama',
                     'rules'   => 'trim|required|xss_clean|callback__checkCompAva['
					 										.$this->input->post('telp').']'
                  ),
               	array(
                     'field'   => 'alamat',
                     'label'   => 'Alamat',
                     'rules'   => 'trim|required|xss_clean'
                  ),
			 	array(
                     'field'   => 'telp',
                     'label'   => 'Telepon',
                     'rules'   => 'trim|required|xss_clean'									
                  )
				
            );

		$this->form_validation->set_rules($config); 
		if ($this->form_validation->run() == FALSE){
			return false;
		}
		else{
			//save to database..
			$this->model_owner->addOwner();
			return true;
		}
	}
	
	function _checkCompAva($namaComp, $noTelp){
		$benar = 0;
		$data = $this->model_owner->getDataComp($namaComp, $noTelp);
		if($data){
			return true	;
		}else{
			return false;
		}
	}
	
	/*
			=================Function View Driver=================
	*/
	public function view(){
		$data['masterview'] = 'y';
		$data['viewdriver'] = 'y';
		$data['title'] = TITLE.' - Lihat Daftar Supir'; 
		$data['driver'] = $this->model_driver->getDriverList(); 
		$this->load->view("driverview",$data);
	}
	
	/*
			=================Function EditDriver=================
	*/
	function edit(){
		$data['masterview'] = 'y';
		$data['viewdriver'] = 'y';
		$id = $this->uri->segment(3, 0);
		$sFlag =  $this->uri->segment(4, 0);
		$data['driverDet'] = $this->model_driver->getDriverDet($id);
		$data['title'] = TITLE.' - Ubah Data Supir'; 
		$data['id'] = $id;
		if($this->input->post("btnSave")){
			$hasil = $this->_editDriver($id);
			if($hasil) {
				$data['success'] = "Data Berhasil Di Ubah";
				redirect('/driver/edit/' .$id .'/s', 'refresh');
			}
		}
		if($sFlag!=""){
			$data['success'] = "Data Berhasil Di Ubah";
		}
		$this->load->view("driveredit",$data);	
	}
	
	function _editDriver($id){
		$this->form_validation->set_error_delimiters('', '');
		$config = array(
               	array(
                     'field'   => 'nama_supir',
                     'label'   => 'Nama Supir',
                     'rules'   => 'trim|required|xss_clean'
                  ),
               	array(
                     'field'   => 'gaji',
                     'label'   => 'Gaju/hari',
                     'rules'   => 'trim|required|xss_clean|integer'
                  ),
			 	array(
                     'field'   => 'alamat',
                     'label'   => 'Alamat',
                     'rules'   => 'trim|xss_clean'									
                  ),
				array(
                     'field'   => 'no_telp',
                     'label'   => 'Nomor Telepon',
                     'rules'   => 'trim|required|xss_clean'
                  ),
               	array(
                     'field'   => 'no_ktp',
                     'label'   => 'Nomor KTP',
                     'rules'   => 'trim'
                  ),
				 array(
                     'field'   => 'umur',
                     'label'   => 'Umur',
                     'rules'   => 'trim|integer'
                  )
				
            );

		$this->form_validation->set_rules($config); 
		if ($this->form_validation->run() == FALSE){
			return false;
		}
		else{
			//save to database..
			$this->model_driver->editDriver($id);
			return true;
		}
	}
	
	/*
			=================Function Delete Driver=================
	*/
	function delete(){
		$id = $this->uri->segment(3, 0);
		$this->model_driver->delDataDriver($id);
		redirect('/driver/view');
	}
}